package com.hmyd.hmyd_android.view;

import com.hmyd.hmyd_android.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

public class TriangleRateView extends View {
	private Paint mPaint;
	/**
	 * X轴Y轴长度。
	 */
	private int XLen, YLen;
	/**
	 * 交叉点坐标中心点
	 */
	private PointF Pointp = new PointF();
	/**
	 * X轴显示个数
	 */
	private int XNum = 4;
	
	/**
	 * Y轴显示个数
	 */
	private int YNum = 0;
	
	/**
	 * path构成一个三角形
	 */
	private Path mPath;
	/**
	 * 三角形的宽度
	 */
	private float mTriangleWidth = 40;
	/**
	 * 三角形的高度
	 */
	private float mTriangleHeight = 80;
	
	private int changedPosition = -1;
	private CharSequence changedText;
	
	private String[] name = {"银行存款","存钱罐","银行理财","国泰惠民益贷"};

	private double[] rate = {3.26, 4.5, 5.28, 18.8};
	private int[] colors = {R.color.login_btn_bg, R.color.cl2, R.color.cl3, R.color.text_orange};
	
	private int text_color = R.color.recommendation_sidemenu_bg;
	private int line_color = R.color.more_line_bg;
	
	public TriangleRateView(Context context) {
		this(context, null, 0);
	}

	public TriangleRateView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TriangleRateView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		
		XNum = getName().length;
		YNum = getYNum();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
	}
	
	private int getYNum(){
		double temp = 0;
		for (int i = 0; i < rate.length; i++) {
			if (temp <= rate[i]) {
				temp = rate[i];
			}
		}
		
		return (int) (temp / 5) + 1;
	}

	/**
	 * 获取中心坐标及X,Y轴长度
	 */
	public void onSizeChanged(int w, int h, int oldw, int oldh) {
		XLen = w - getPaddingLeft() - getPaddingRight();
		YLen = h - getPaddingBottom() - getPaddingTop();
		Pointp.set(getPaddingLeft(), h - getPaddingBottom());
		
		super.onSizeChanged(w, h, oldw, oldh);
	}

	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (canvas == null) {
			return;
		}
		canvas.drawColor(Color.WHITE);
		
		mPaint.setColor(getResources().getColor(text_color));
		mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11, getResources().getDisplayMetrics()));
		
	    int textPoint = XLen / (XNum + 1);
		
	    // X轴对应的文字
	    for (int i = 0; i < XNum; i++) {
	    	float width = mPaint.measureText(getName()[i]);
			
	    	canvas.drawText(getName()[i], Pointp.x + textPoint * (i + 1) - width / 2, Pointp.y + getPaddingBottom() / 2, mPaint);
		}
	    
	    // Y轴对应的文字
	    int rate_ = 0;
		for (int i = 0; i < YNum+1; i++) {
			canvas.drawText(rate_ + "%",  Pointp.x - getPaddingLeft(),  Pointp.y * (YNum+1-i) / (YNum + 1), mPaint);
			rate_ = rate_ + 5;
		}
//		canvas.drawText(" 0%",  Pointp.x - getPaddingLeft(),  Pointp.y, mPaint);
//		canvas.drawText(" 5%",  Pointp.x - getPaddingLeft(),  Pointp.y * 4 / 5, mPaint);
//		canvas.drawText("10%", Pointp.x - getPaddingLeft(), Pointp.y * 3 / 5, mPaint);
//		canvas.drawText("15%", Pointp.x - getPaddingLeft(), Pointp.y * 2 / 5, mPaint);
//		canvas.drawText("20%", Pointp.x - getPaddingLeft(), Pointp.y / 5, mPaint);
		
	    // 设置线颜色
		mPaint.setColor(getResources().getColor(line_color));
		
		// Y轴 对应的线
		for (int i = 0; i < YNum; i++) {
			canvas.drawLine(Pointp.x, Pointp.y * (YNum-i) / (YNum + 1), Pointp.x + XLen, Pointp.y * (YNum-i) / (YNum + 1), mPaint);
		}
//		canvas.drawLine(Pointp.x, Pointp.y * 4 / 5, Pointp.x + XLen, Pointp.y * 4 / 5, mPaint);
//		canvas.drawLine(Pointp.x, Pointp.y * 3 / 5, Pointp.x + XLen, Pointp.y * 3 / 5, mPaint);
//		canvas.drawLine(Pointp.x, Pointp.y * 2 / 5, Pointp.x + XLen, Pointp.y * 2 / 5, mPaint);
//		canvas.drawLine(Pointp.x, Pointp.y / 5, Pointp.x + XLen, Pointp.y / 5, mPaint);
		
		// 画X轴
		canvas.drawLine(Pointp.x, Pointp.y, Pointp.x + XLen, Pointp.y, mPaint);
		// 画Y轴
		canvas.drawLine(Pointp.x, Pointp.y, Pointp.x, Pointp.y - YLen, mPaint);
	}
	
	/**
	 * 绘制三角形视图
	 */
	@Override
	protected void dispatchDraw(Canvas canvas) {
		mTriangleWidth = (int) mPaint.measureText(getName()[0]);
		int textPoint = XLen / (XNum + 1);
		
		for (int i = 0; i < XNum; i++) {
			mTriangleHeight = (float) (Pointp.y * rate[i] / (YNum + 1) / 5);
			initTriangle(mTriangleHeight);

			float x = Pointp.x + textPoint * (i + 1);
			float y = Pointp.y;
			
			canvas.save();
			if (i == rate.length - 1) {
				mPaint.setColor(getResources().getColor(colors[i]));
			} else {
				mPaint.setColor(getResources().getColor(text_color));
			}
			if(changedPosition == i) {
				if(!TextUtils.isEmpty(changedText)) {
					canvas.drawText((String) changedText, x - mPaint.measureText(rate[i]+"%")/2, y - mTriangleHeight - 5, mPaint);
				} else {
					canvas.drawText(rate[i]+"%", x - mPaint.measureText(rate[i]+"%")/2, y - mTriangleHeight - 5, mPaint);
				}
			} else {
				canvas.drawText(rate[i]+"%", x - mPaint.measureText(rate[i]+"%")/2, y - mTriangleHeight - 5, mPaint);
			}
			
			
			mPaint.setColor(getResources().getColor(colors[i]));
			// 画笔平移到正确的位置
			canvas.translate(x - mTriangleWidth/2, y);
			canvas.drawPath(mPath, mPaint);
			canvas.restore();
		}
		
		super.dispatchDraw(canvas);
	}
	
	/**
	 * 初始化三角形指示器
	 */
	private void initTriangle(float mTriangleHeight)
	{
		mPath = new Path();

		mPath.moveTo(0, 0);
		mPath.lineTo(mTriangleWidth, 0);
		mPath.lineTo(mTriangleWidth / 2, -mTriangleHeight);
		mPath.close();
	}
	
	/**
	 * 修改指定位置的label
	 * @param position
	 * @param text
	 */
	public void setPositionLabel(int position,CharSequence text) {
		this.changedPosition = position;
		this.changedText = text;
	}
	
	public String[] getName() {
		return name;
	}

	public void setName(String[] name) {
		this.name = name;
		this.XNum = name.length;
	}

	public double[] getRate() {
		return rate;
	}

	public void setRate(double[] rate) {
		this.rate = rate;
		this.YNum = getYNum();
	}

	public int[] getColors() {
		return colors;
	}

	public void setColors(int[] colors) {
		this.colors = colors;
	}
}